﻿package com.tomvian.twodee.common.actions
{
	import sfb.engines.core.control.Controller;
	import sfb.engines.core.GameCamera;
	import com.tomvian.twodee.common.Actor;
	import com.tomvian.twodee.common.StateDisplay;
	import com.tomvian.maths.Vector2D;
	import sfb.physics.Mass;
	
	/**
	 * Action
	 * @author Thomas Vian
	 */
	public class Action 
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		protected var _enabled:Boolean = true;
		
		public var actor:Actor;
		public var actorMass:Mass;
		public var position:Vector2D;
		public var velocity:Vector2D;
		
		public var actorController:Controller;
		public var actorState:StateDisplay;
		
		public var cameraOffset:Vector2D;
		
		public var state:uint;
		public var allowedStates:Array;
		
		public var next:Action;
		
		protected var _branchActions:Action;
		protected var _branchAction:Action;
		
		//--------------------------------------------------------------------------
		//	
		// Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		public function get enabled():Boolean { return _enabled; }
		public function set enabled(value:Boolean):void
		{
			_enabled = value;
			//if (!_enabled && actor.currentAction == this && actor.defaultAction != this) actor.returnToDefault();
		}
		
		//--------------------------------------------------------------------------
		//	
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function Action(actor:Actor)
		{
			this.actor = actor;
			this.actorMass = actor.mass;
			this.position = this.actorMass.position;
			this.velocity = this.actorMass.velocity;
			this.actorController = actor.controller;
			this.actorState = actor.actionDisplay;
			this.cameraOffset = actor.cameraOffset
			
			this.allowedStates = [];
		}
		
		//--------------------------------------------------------------------------
		//	
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function addBranchAction(action:Action, allowedStates:Array = null):void
		{
			action.next = _branchActions;
			_branchActions = action;
			_branchActions.allowedStates = allowedStates || [0];
		}
		
		public function checkBranchActions():void
		{
			_branchAction = _branchActions;
			
			while (_branchAction)
			{
				if (_branchAction.enabled && _branchAction.allowedStates.indexOf(state) != -1) 
				{
					_branchAction.check(state);
				}
				
				_branchAction = _branchAction.next;
			}
		}
		
		public function check(state:uint):void
		{
			
		}
		
		public function start():void
		{
			
		}
		
		public function stop():void
		{
			
		}
		
		public function act():void
		{
			
		}
		
		public function display():void
		{
			
		}
	}
}